using System;
using System.Collections.Generic;
using System.Text;
using Sixeyed.CacheAdapter.Interfaces;

namespace Sixeyed.CacheAdapter
{
    /// <summary>
    /// Factory class for returning an initialized cache instance
    /// </summary>
    public static class CacheFactory
    {
        /// <summary>
        /// Returns an initialized cache provider
        /// </summary>
        /// <param name="implementingTypeName">Fully-qualified type name of the cache provider</param>
        /// <param name="configuration">Configuration of the cache instance</param>
        /// <returns>Initialized cache provider</returns>
        public static ICacheProvider GetProvider(string implementingTypeName, ICacheProviderConfiguration configuration)
        {
            Type implementingType = Type.GetType(implementingTypeName);
            return GetProvider(implementingType, configuration);
        }

        /// <summary>
        /// Returns an initialized cache provider
        /// </summary>
        /// <param name="implementingType">Type of the cache provider</param>
        /// <param name="configuration">Configuration of the cache instance</param>
        /// <returns>Initialized cache provider</returns>
        public static ICacheProvider GetProvider(Type implementingType, ICacheProviderConfiguration configuration)
        {
            ICacheProvider provider = Activator.CreateInstance(implementingType) as ICacheProvider;
            if (provider != null)
            {
                provider.Initialize(configuration);
            }
            return provider;
        }
    }
}
